package cn.gameback.supervisionservices.service.impl;

import cn.gameback.supervisionservices.entity.Supersition;
import cn.gameback.supervisionservices.entity.User;
import cn.gameback.supervisionservices.mapper.SupersitionMapper;
import cn.gameback.supervisionservices.service.SupersitionService;
import cn.gameback.supervisionservices.vo.RequestVo.SupervisionVo.SupervisionQuery;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class SupersitionServiceImpl extends ServiceImpl<SupersitionMapper, Supersition> implements SupersitionService {
    @Autowired
    private SupersitionMapper supersitionMapper;

    @Override
    public IPage<Supersition> pagingQuerySupervision(SupervisionQuery supervisionQueryRequest) {
        Page<Supersition> supersitionPage = new Page<>(supervisionQueryRequest.getCurrent(), supervisionQueryRequest.getPageSize());
        QueryWrapper<Supersition> queryWrapper = new QueryWrapper<>();

        // 模糊搜索 上课地点
        if (supervisionQueryRequest.getClassroomLocation() != null && !supervisionQueryRequest.getClassroomLocation().isEmpty()) {
            queryWrapper.like("classroomLocation", supervisionQueryRequest.getClassroomLocation());
        }

        // 模糊搜索 课程名
        if (supervisionQueryRequest.getCourseName() != null && !supervisionQueryRequest.getCourseName().isEmpty()) {
            queryWrapper.like("courseName", supervisionQueryRequest.getCourseName());
        }

        // 模糊搜索 上课班级
        if (supervisionQueryRequest.getClassNumber() != null && !supervisionQueryRequest.getClassNumber().isEmpty()) {
            queryWrapper.like("classNumber", supervisionQueryRequest.getClassNumber());
        }


        // 模糊搜索 上课教师
        if (supervisionQueryRequest.getInstructor() != null && !supervisionQueryRequest.getInstructor().isEmpty()) {
            queryWrapper.like("instructor", supervisionQueryRequest.getInstructor());
        }

        // 通过 时间区间选择
        if (supervisionQueryRequest.getStartTime() != null && supervisionQueryRequest.getEndTime() != null) {
            LocalDateTime startDate = LocalDateTime.parse(supervisionQueryRequest.getStartTime());
            LocalDateTime endDate = LocalDateTime.parse(supervisionQueryRequest.getEndTime());

            System.out.println("时间区间为 >>> " + startDate + endDate);
            queryWrapper.between("created_at", startDate, endDate);
        }

        // 返回查询结果
        return supersitionMapper.selectPage(supersitionPage, queryWrapper);
    }
}
